// 02order/order.js
var ws = require("../resource/js/websocket.js")
var co = require("../resource/js/common.js")
var MAP = require('../resource/js/map.js')
var app = getApp()
var _this, _data
var _pageNum1, _RoomID
Page({
   data: {
      orderTabChange: 0, //已接订单和已完订单切换
      nowAddress: '', //当前位置
      latAndlng: [],
      orderList: [], //订单信息
      // 音频地址
      audioSrc: '',
      showNoOrderImg: true,
      openTimeborder: false,
      timeArray: [
         [
            "09时", "10时", "11时", "12时", "13时", "14时", "15时", "16时",
            "17时", "18时", "19时"
         ],
         [
            "00分", "01分", "02分", "03分", "04分", "05分", "06分", "07分", "07分", "08分", "09分", "10分", "11分", "12分", "13分", "14分", "15分", "16分", "17分", "18分", "19分", "20分", "21分", "22分", "23分", "24分", "25分", "26分", "27分", "28分", "29分", "30分", "31分", "32分", "33分", "34分", "35分", "36分", "37分", "38分", "39分", "40分", "41分", "42分", "43分", "44分", "45分", "46分", "47分", "48分", "49分", "50分", "51分", "52分", "53分", "54分", "55分", "56分", "57分", "58分", "59分"
         ],
      ],
      showLoCation: false,
   },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(opt) {
      _this = this
      _data = _this.data
      _pageNum1 = 1
      if (!co.empty(app.Data.userInfo)) {
         _RoomID = "jyhs_b"
         ws.initWebsocket(app.Data.userInfo, _RoomID, function(res) {
            console.log('【WS初始化及登录结果】', res)
         })
         _this.initWS_Login()
      }
      console.log(app.Data)
   },
   onShow: function() {
      _pageNum1 = 1
      _this.setData({
         orderTabChange: 0,
      })
      _this.orderList(1)
      _this.getUserMap()
   },
   onReady: function() {
      _this.audioCtx = wx.createAudioContext('myAudio')
   },
   //ws初始化
   initWS_Login() {
      //监听WS
      ws.onSocketMsg(function(res) {
         console.log("连接成功", res)
         //企业
         if (res.type == "getBizOrder") {
            if (res.to_user_id > 0) {
               if (app.Data.userInfo.User_ID == res.to_user_id) {
                  _this.playMP3(res.content, 1)
                  _pageNum1 = 1
                  _this.setData({
                     orderList: [],
                     showNoOrderImg: true
                  })
                  _this.orderList(1)
                  var msg = {
                     type: "getBizOrder_Notify",
                     to_biz_id: res.from_biz_id
                  }
                  ws.sendSocketMsg(JSON.stringify(msg), function(res) {
                     console.log("getBizOrder_Notify===", res)
                  })
               }
            }
         } else if (res.type == "getBizOrder_Notify") {
            _this.playMP3(res.content, 1)
            _pageNum1 = 1
            _this.orderList(1)
         } else if (res.type == "getUserOrder") { //接收用户订单
            _pageNum1 = 1
            _this.setData({
               orderList: [],
               showNoOrderImg: true
            })
            _this.orderList(1)
            if (res.to_user_id == app.Data.userInfo.User_ID) {
               _this.playMP3(res.content, 1)
               var msg = {
                  type: "getUserOrder_Notify",
                  to_user_id: res.from_user_id
               }
               ws.sendSocketMsg(JSON.stringify(msg), function(res) {
                  console.log("getUserOrder_Notify===", res)
               })
            }
         } else if (res.type == "cancelOrder") { //接收取消订单的
            _pageNum1 = 1
            _this.setData({
               orderList: [],
            })
            _this.orderList(1)
            if (res.to_user_id == app.Data.userInfo.User_ID) {
               _this.playMP3(res.content, 4)
            }
         } else if (res.type == "finishOrder") { //完成订单
            _this.setData({
               orderList: [],
               orderList: []
            })
            _pageNum1 = 1
            _this.orderList(1)
            if (res.to_user_id == app.Data.userInfo.User_ID) {
               _this.playMP3(res.content, 6)
            }
         }
      })
   },
   onUnload: function() {
      _pageNum1 = 1
   },
   //点击切换标签
   changeTab: function(e) {
      _this.setData({
         orderTabChange: e.currentTarget.id,
         showNoOrderImg: true
      })
      _pageNum1 = 1
      var stutas = parseInt(e.currentTarget.id) + 1
      if (stutas == 2) {
         stutas = -1
      }
      _this.orderList(stutas)
   },
   //获取订单列表
   orderList(OrderStatus) {
      _this.setData({
         orderList: []
      })
      co.getJyhsApi("order/Order/getAllOrderList", {
         Reclaim_User_ID: app.Data.userInfo.User_ID,
         Order_Status: OrderStatus,
         page: _pageNum1
      }, function(res) {
         if (res.code == 0) {
            var list = _data.orderList.concat(res.data)
            if (res.data.length > 0) {
               _this.setData({
                  orderList: list
               })
               console.log(_data.orderList)
               _pageNum1++
            } else {
               _this.setData({
                  showNoOrderImg: false
               })
            }
         } else {
            _this.setData({
               showNoOrderImg: false
            })
         }
      })
   },
   //获取用户位置
   getUserMap() {
      MAP.wxGetLocation(function(res) {
         if (res.latitude) {
            //将获取到的经纬度存入latAndlng
            var lat_lng = _data.latAndlng
            var lng = res.longitude ? res.longitude : app.Data.p.longitude
            var lat = res.latitude ? res.latitude : app.Data.p.latitude
            // app.Data.p.longitude = lng
            // app.Data.p.latitude = lat
            lat_lng.push(lng)
            lat_lng.push(lat)
            // 调用获取附近商家方法
            var param = {
               longitude: lng,
               latitude: lat,
            }
            // 坐标转中文地址
            MAP.getLocationName(param, function(res) {
               console.log("【获取返回的中文地址】===", res)
               _this.setData({
                  nowAddress: res.result.address_reference.landmark_l2.title,
                  latAndlng: lat_lng,
                  showLoCation: false
               })
               console.log("_data.latAndlng", _data.latAndlng)
               //经度纬度更新到数据库
               _this.updatelag()
            })
         } else {
            _this.setData({
               showLoCation: true
            })
         }
      })
   },
   //重新获取位置
   reGet() {
      MAP.askLocationAuth(function(res) {
         if (res) {
            _this.getUserMap()
         }
      })
   },
   //跳转至订单详情
   gotoOrderDetail(e) {
      var index = e.currentTarget.id
      var obj = _data.orderList[index]
      wx.navigateTo({
         url: '/02order/orderDetail/orderDetail?Order_ID=' + JSON.stringify(obj),
      })
   },
   openTime() {
      _this.setData({
         openTimeborder: true
      })
   },
   cancelTime() {
      _this.setData({
         openTimeborder: false
      })
   },
   //弹出时间设定框
   bindTimeChange(e) {
      var index = e.currentTarget.id
      var obj = _data.orderList[index]
      var value = e.detail.value
      var hour = _data.timeArray[0][value[0]].split("时")
      hour = hour[0]
      var minite = _data.timeArray[1][value[1]].split("分")
      minite = minite[0]
      // 年月日
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      if (month < 10) {
         month = "0" + month;
      }
      if (day < 10) {
         day = "0" + day;
      }
      var nowDate1 = year + "-" + month + "-" + day;
      var nowDate = nowDate1 + " " + hour + ":" + minite + ":" + "00"
      var param = {
         Order_ID: obj.Order_ID,
         Appoint_Drop_Time: nowDate
      }
      _this.setAppointTime(param, obj)
   },
   //设定预约上门时间
   setAppointTime(param, obj) {
      co.getJyhsApi("order/Order/setAppointTime", param, function(res) {
         if (res.code == 0) {
            _this.setData({
               openTimeborder: false
            })
            _this.updataOrderStatus(obj)
         }
      })
   },
   //更新订单状态
   updataOrderStatus(obj) {
      co.getJyhsApi("order/Order/updateOrderStatue", {
         Order_ID: obj.Order_ID,
         Order_Status: 5
      }, function(res) {
         if (res.code == 0) {
            wx.navigateTo({
               url: '/02order/orderDetail/orderDetail?Order_ID=' + JSON.stringify(obj),
            })
         }
      })
   },
   //语音播报
   playMP3: function(data, types) {
      //判断是否是换桌
      co.getJyhsApi('api/Voice/voiceRecognite', {
         vol: 5,
         message: data,
         type: types
      }, function(res) {
         _this.setData({
            audioSrc: res.data
         })
         setTimeout(function() {
            _this.nowPlay()
         }, 100)
      })
   },
   nowPlay: function(res) {
      _this.audioCtx.play()
   },
   //经度纬度更新到数据库
   updatelag() {
      co.getShareApi("jyhs/Usermg/updateInfo", {
         User_ID: app.Data.userInfo.User_ID,
         User_PrimaryLng: _data.latAndlng[0], //经度
         User_PrimaryLat: _data.latAndlng[1] //纬度
      }, function(res) {
         console.log(res.msg)
      })
   },
   //拒接订单
   refuse_order(e) {
      var index = e.currentTarget.id
      var Order_ID = _data.orderList[index].Order_ID
      co.getJyhsApi("order/Order/rejectOrder", {
         Order_ID: Order_ID,
         Reclaim_User_ID: app.Data.userInfo.User_ID
      }, function(res) {
         if (res.code == 0) {
            co.toastAllText(res.msg)
            _pageNum1 = 1
            _this.orderList(1)
         } else {
            co.toastAllText(res.msg)
         }
      })
   },
})